package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.Orders;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 订单管理控制层
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 下单
     *
     * session:为了得到用户id 和 用户名 保存到orders订单表中
     */
    @PostMapping("/submit")
    public R<String> save(@RequestBody Orders orders, HttpSession session){
        orderService.save(orders,session);
        return R.success("下单成功了");
    }

    /**
     * 订单分页查询
     */
    @GetMapping("/page")
    public R<PageResult>list(@RequestParam(value = "page",defaultValue = "1")Long page,
                             @RequestParam(value = "pageSize",defaultValue = "10")Long pageSize,
                             String number,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime){
        PageResult<Orders> pageResult =orderService.list(page,pageSize,number,beginTime,endTime);
        return R.success(pageResult);
    }

    /**
     * 订单 派送/已完成状态修改
     */
    @PutMapping
    public R<String>updateOrder(@RequestBody Orders orders, HttpSession session){
        orderService.updateOrder(orders,session);
        return R.success("订单派送完成");
    }

    /**
     * 移动端-历史订单/最新订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<PageResult> userPage(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "pageSize", defaultValue = "5") Long pageSize) {
        PageResult<OrdersDto> pageResult = orderService.userPage(page, pageSize);
        return R.success(pageResult);
    }

}
